package de.g18.llmaa.gui.workspace.internalFrame.lizenzUebersicht.treeTable;

/*
 * %W% %E%
 *
 * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer. 
 *   
 * - Redistribution in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials
 *   provided with the distribution. 
 *   
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.  
 * 
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 
 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,   
 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER  
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 
 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import de.g18.llmaa.gui.lizenzTree.TreeNode_Key;
import de.g18.llmaa.gui.lizenzTree.TreeNode_Product;
import de.g18.llmaa.logic.vos.VOProduct_Key;

/**
 * FileSystemModel is a TreeTableModel representing a hierarchical file system.
 * Nodes in the FileSystemModel are FileNodes which, when they are directory
 * nodes, cache their children to avoid repeatedly querying the real file
 * system.
 * 
 * @version %I% %G%
 * 
 * @author Philip Milne
 * @author Scott Violet
 */

public class LizenzTreeTableModel extends AbstractTreeTableModel implements
		TreeTableModel {

	// Names of the columns.
	static protected String[] cNames = { "Name", "Datum", "ID", "Typ" };

	// Types of the columns.
	static protected Class[] cTypes = { TreeTableModel.class, String.class,
			String.class, String.class };

	// The the returned file length for directories.
	public static final Integer ZERO = new Integer(0);



	public LizenzTreeTableModel(Object root) {
		super(root);
		
	}

	protected VOProduct_Key getDimension(Object node) {
		if (node instanceof TreeNode_Product) {
			return ((TreeNode_Product) node).getProduct();
		}
		if (node instanceof TreeNode_Key) {
			return ((TreeNode_Key) node).getKey().getKey_Product();
		}
		// nue
		return null;
	}

	protected Object[] getChildren(Object node) {

		MyAbstractTreeNode fileNode = ((MyAbstractTreeNode) node);
		return fileNode.getChildren();
	}

	//
	// The TreeModel interface
	//

	public int getChildCount(Object node) {
		Object[] children = getChildren(node);
		return (children == null) ? 0 : children.length;
	}

	public Object getChild(Object node, int i) {
		return getChildren(node)[i];
	}

	// The superclass's implementation would work, but this is more efficient.
	public boolean isLeaf(Object node) {
		if (node instanceof TreeNode_Product)
			return false;
		if (node instanceof TreeNode_Key)
			return true;
		else
			return false;
	}

	//
	// The TreeTableNode interface.
	//

	public int getColumnCount() {
		return cNames.length;
	}

	public String getColumnName(int column) {
		return cNames[column];
	}

	public Class getColumnClass(int column) {
		return cTypes[column];
	}

	public Object getValueAt(Object node, int column) {

		// VOProduct_Key file = getDimension(node);
		try {
			if (node instanceof TreeNode_Key) {
				TreeNode_Key tempNode = (TreeNode_Key) node;
				switch (column) {
				case 0:

					return "";
				case 1: {


					if (tempNode.getKey().getKey_ClaimedDate() != null) {
//						Calendar cal = new GregorianCalendar(TimeZone
//								.getTimeZone("ECT"));
						SimpleDateFormat formater = new SimpleDateFormat();
						formater.applyPattern("dd.MM.yyyy");
						//

						return ""
								+ formater.format(tempNode.getKey()
										.getKey_ClaimedDate());
					}
					return "";
				}

				case 2:
		
					return "" + tempNode.getKey().getKey_Id();

				case 3:
					return "" + tempNode.getKey().getKey_Type().getType_Name();

				}
			}
		} catch (SecurityException se) {
		}

		return null;
	}
}

/*
 * A FileNode is a derivative of the File class - though we delegate to the File
 * object rather than subclassing it. It is used to maintain a cache of a
 * directory's children and therefore avoid repeated access to the underlying
 * file system during rendering.
 */
// class FileNode extends DefaultMutableTreeNode {
// VOProduct_Key product;
// Object[] children;
//
// public FileNode(VOProduct_Key product) {
// this.product = product;
// }
//
// // Used to sort the file names.
// static private MergeSort fileMS = new MergeSort() {
// public int compareElementsAt(int a, int b) {
// return ((String)toSort[a]).compareTo((String)toSort[b]);
// }
// };
//
// /**
// * Returns the the string to be used to display this leaf in the JTree.
// */
// public String toString() {
// return product.getProduct_Name();
// }
//
// public VOProduct_Key getProduct() {
// return product;
// }
//
// /**
// * Loads the children, caching the results in the children ivar.
// */
// protected Object[] getChildren() {
// if (children != null) {
// return children;
// }
// try {
// String[] files = (String[]) product.getList_Key().toArray();
// if(files != null) {
// // fileMS.sort(files);
// children = new TreeNode_Key[files.length];
// // String path = file.getPath();
// for(int i = 0; i < files.length; i++) {
// // File childFile = new File(path, files[i]);
// children[i] = new TreeNode_Key((VOKey) product.getList_Key().toArray()[i]);
// }
// }
// } catch (SecurityException se) {}
// return children;
// // super.getc;
// }
// }

